package com.bing.jh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.util.ProvinceUtil;
import com.bing.jh.entity.MdSoftProbeRelStats;
import com.bing.jh.entity.dto.MdSoftProbeRelStatsDTO;
import com.bing.jh.entity.vo.MdSoftProbeRelStatsVO;
import com.bing.jh.mapper.MdSoftProbeRelStatsMapper;
import com.bing.jh.service.MdSoftProbeRelStatsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 29821
 * @description 针对表【md_soft_probe_rel_stats(关联率)】的数据库操作Service实现
 * @createDate 2024-04-12 11:17:06
 */
@Service
public class MdSoftProbeRelStatsServiceImpl extends ServiceImpl<MdSoftProbeRelStatsMapper, MdSoftProbeRelStats>
        implements MdSoftProbeRelStatsService {

    @Resource
    private MdSoftProbeRelStatsMapper mdSoftProbeRelStatsMapper;

    @Override
    public List<MdSoftProbeRelStatsVO> list(MdSoftProbeRelStatsDTO infoDTO) {
        String dt = infoDTO.getDt().replace("-", "");
        String province = ProvinceUtil.getKeyProvince(infoDTO.getProvince());
        List<MdSoftProbeRelStatsVO> statsVOList = mdSoftProbeRelStatsMapper.findProvinceList(dt, province);
        MdSoftProbeRelStatsVO cnVo = mdSoftProbeRelStatsMapper.findCnList(dt, province);
        statsVOList.add(0, cnVo);
        statsVOList.forEach(statsVO -> {
            if (!"全国".equals(statsVO.getProvince())){
                statsVO.setProvince(ProvinceUtil.getProvince(statsVO.getProvince()));
            }
            statsVO.setStbDailyRate(addPercentage(statsVO.getStbDailyRate()));
            statsVO.setZnIhguRate(addPercentage(statsVO.getZnIhguRate()));
            statsVO.setStbIhguFullRate(addPercentage(statsVO.getStbIhguFullRate()));
            statsVO.setZnIhguFullRate(addPercentage(statsVO.getZnIhguFullRate()));
            statsVO.setIhguUqpRate(addPercentage(statsVO.getIhguUqpRate()));
            statsVO.setStbUqpRate(addPercentage(statsVO.getStbUqpRate()));
            statsVO.setZnUqpRate(addPercentage(statsVO.getZnUqpRate()));
        });

        return statsVOList;
    }

    @Override
    public List<String> dateList() {
        return mdSoftProbeRelStatsMapper.dateList();
    }

    @Override
    public List<String> provinceList() {
        List<String> list = mdSoftProbeRelStatsMapper.provinceList().stream()
                .sorted(Comparator.comparing(String::valueOf))
                .map(ProvinceUtil::getProvince)
                .collect(Collectors.toList());
        list.add(0,"全部");
        return list;
    }

    private String addPercentage(String rate) {
        return rate != null ? rate + "%" : null;
    }
}




